package com.example.peibei.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.dingtao.common.bean.Spread;
import com.dingtao.common.bean.User;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.bean.SpUserInfoCall;
import com.example.peibei.service.presenter.SpreadPresenter;
import com.example.peibei.service.presenter.UserInfoPresenter;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TuiguangActivity extends WDActivity {
    @BindView(R.id.tv_person_one)
    TextView tv_person_one;
    @BindView(R.id.tv_person_two)
    TextView tv_person_two;
    @BindView(R.id.tv_person_three)
    TextView tv_person_three;
    @BindView(R.id.tv_scale_one)
    TextView tv_scale_one;
    @BindView(R.id.tv_scale_two)
    TextView tv_scale_two;
    @BindView(R.id.tv_scale_three)
    TextView tv_scale_three;
    @BindView(R.id.tv_serviceCount)
    TextView tv_serviceCount;
    @BindView(R.id.tv_profitMoney)
    TextView tv_profitMoney;
    @BindView(R.id.tv_scale)
    TextView tv_scale;
    @BindView(R.id.tv_id)
    TextView tv_id;
    @BindView(R.id.tv_people_one)
    TextView tv_people_one;
    @BindView(R.id.tv_people_two)
    TextView tv_people_two;
    @BindView(R.id.tv_people_three)
    TextView tv_people_three;
    @BindView(R.id.tv_money_one)
    TextView tv_money_one;
    @BindView(R.id.tv_money_two)
    TextView tv_money_two;
    @BindView(R.id.tv_money_three)
    TextView tv_money_three;

    private SpreadPresenter spreadPresenter;
    private UserInfoPresenter userInfoPresenter;
    private SPUtils spUtils;
    private String token;
    private int personCount;
    private double profitMoney;
    private int serviceCount;
    private String scale;
    private String nickName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tui_guang;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this)
                .fitsSystemWindows(false)
                .statusBarDarkFont(false)
                .hideBar(BarHide.FLAG_HIDE_STATUS_BAR)
                .init();
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        spreadPresenter = new SpreadPresenter(new SpreadCall());
        userInfoPresenter = new UserInfoPresenter(new UserInfoCall());
        userInfoPresenter.reqeust("Bearer " + token);
    }

    @Override
    protected void onResume() {
        super.onResume();
        spreadPresenter.reqeust("Bearer " + token);
    }

    @OnClick(R.id.tv_guize)
    public void guize() {
        intent(TuiguangGuizeActivity.class);
    }

    @OnClick(R.id.tv_fuzhi)
    public void fuzhi() {
        copyStr(tv_id.getText().toString());
        UIUtils.showToastSafe("已复制到剪切板");
    }

    /**
     * 复制内容到剪切板
     *
     * @param copyStr
     * @return
     */
    private boolean copyStr(String copyStr) {
        try {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", copyStr);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @OnClick(R.id.tv_tuiguang_detail)
    public void tuiguangdetail() {
        Bundle bundle = new Bundle();
        bundle.putInt("personCount", personCount);
        bundle.putDouble("profitMoney", profitMoney);
        bundle.putString("scale", scale);
        bundle.putString("nickName", nickName);
        bundle.putInt("serviceCount", serviceCount);
        intent(TuiguangDetailActivity.class, bundle);
    }

    class SpreadCall implements DataCall<Spread> {



        @Override
        public void success(Spread data, Object... args) {
            List<Spread.DescListDTO> descList = data.getDescList();
            tv_person_one.setText(descList.get(0).getConditions() + "");
            tv_scale_one.setText(descList.get(0).getScale() + "");
            tv_person_two.setText(descList.get(1).getConditions() + "");
            tv_scale_two.setText(descList.get(1).getScale() + "");
            tv_person_three.setText(descList.get(2).getConditions() + "");
            tv_scale_three.setText(descList.get(2).getScale() + "");
            tv_serviceCount.setText(data.getServiceCount() + "");
            tv_profitMoney.setText(data.getProfitMoney() + "");
            tv_scale.setText(data.getScale() + "");
            List<Spread.ProfitListDTO> profitList = data.getProfitList();
            tv_people_one.setText(profitList.get(0).getName());
            tv_people_two.setText(profitList.get(1).getName());
            tv_people_three.setText(profitList.get(2).getName());
            tv_money_one.setText(profitList.get(0).getMoney() + "");
            tv_money_two.setText(profitList.get(1).getMoney() + "");
            tv_money_three.setText(profitList.get(2).getMoney() + "");
            personCount = data.getPersonCount();
            profitMoney = data.getProfitMoney();
            serviceCount = data.getServiceCount();
            scale = data.getScale();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class UserInfoCall extends SpUserInfoCall {

        @Override
        public void success(User data, Object... args) {
            super.success(data,args);
            nickName = data.getNickName();
            tv_id.setText(data.getAutoId() + "");
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    @Override
    protected void destoryData() {

    }
}
